﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Web.Routing;
using DataAccessLayer;
using DataAccessLayer.Models;

namespace PresentationLayerMVC.Controllers
{
    public class BaseController : StatelessHome
    {
        private static Dictionary<string, BaseAction> routing = new Dictionary<string, BaseAction>();

        private static SelectList IDView = null;
        private static SelectList CtryView = null;
        private static SelectList GenderView = null;
        private static SelectList RelView = null;
        private static SelectList STypeView = null;
        private static SelectList TnrUnitView = null;
        private static SelectList IntTypeView = null;

        //static initialization (loaded only once)
        static BaseController()
        {
            routing.Add(Constants.ACT_LOGIN, new BaseAction(Constants.ACT_HOME, Constants.CONTROLLER_HOME));
            routing.Add(Constants.ACT_CREATE_NEWAPP, new BaseAction(Constants.ACT_ADDAPPLICANT, Constants.CONTROLLER_NEWAPP));
            routing.Add(Constants.ACT_LOGOUT, new BaseAction(Constants.ACT_LOGIN, Constants.CONTROLLER_HOME));

            //Thuzar's Start
            routing.Add(Constants.ACT_ADDAPPLICANT, new BaseAction(Constants.ACT_LISTPRODUCT, Constants.CONTROLLER_PRODUCTS));

            routing.Add(Constants.ACT_ADDFACILITY_HL, new BaseAction(Constants.ACT_CREATE_MORTGAGE, Constants.CONTROLLER_MORTGAGE));
            routing.Add(Constants.ACT_ADDFACILITY_OD, new BaseAction(Constants.ACT_LISTSECURITY, Constants.CONTROLLER_SECURITY));
            routing.Add(Constants.ACT_ADDFACILITY_TL, new BaseAction(Constants.ACT_LISTSECURITY, Constants.CONTROLLER_SECURITY));

            routing.Add(Constants.ACT_ADDSECURITY_LM, new BaseAction(Constants.ACT_LISTSUMMARY, Constants.CONTROLLER_NEWAPP));
            routing.Add(Constants.ACT_ADDSECURITY_FD, new BaseAction(Constants.ACT_LISTSUMMARY, Constants.CONTROLLER_NEWAPP));
            routing.Add(Constants.ACT_ADDSECURITY_PG, new BaseAction(Constants.ACT_LISTSUMMARY, Constants.CONTROLLER_NEWAPP));
            //Thuzar's End

            IDView = new SelectList(EnumeratedType.IDList, EnumeratedType.IDCdLbl, EnumeratedType.IDDescLbl);
            CtryView = new SelectList(EnumeratedType.CtryList, EnumeratedType.CtryCdLbl, EnumeratedType.CtryDescLbl);
            GenderView = new SelectList(EnumeratedType.GenderList, EnumeratedType.GenderCdLbl, EnumeratedType.GenderDescLbl);
            RelView = new SelectList(EnumeratedType.RelList, EnumeratedType.RelCdLbl, EnumeratedType.RelDescLbl);
            STypeView = new SelectList(EnumeratedType.STypeList, EnumeratedType.STypeCdLbl, EnumeratedType.STypeDescLbl);
            TnrUnitView = new SelectList(EnumeratedType.TnrUnitList, EnumeratedType.TnrUnitCdLbl, EnumeratedType.TnrUnitDescLbl);
            IntTypeView = new SelectList(EnumeratedType.IntTypeList, EnumeratedType.IntTypeCdLbl, EnumeratedType.IntTypeDescLbl);
        }

        protected RouteValueDictionary routeValues;

        protected BaseController(){}

        protected ActionResult Next(string prevAction)
        {
            if (routing.ContainsKey(prevAction))
            {
                BaseAction nextAction = routing[prevAction];
                return RedirectToAction(nextAction.action, nextAction.controller);
            }

            return View();
        }

        protected void initDisplay()
        {
            ViewData["IDList"] = IDView;
            ViewData["CountryList"] = CtryView;
            ViewData["GenderList"] = GenderView;
            ViewData["RelationshipsList"] = RelView;
            ViewData["SList"] = STypeView;
            ViewData["TnrUnitList"] = TnrUnitView;
            ViewData["IntTypeList"] = IntTypeView;
        }
    }

    class BaseAction
    {
        public string action;
        public string controller;

        public BaseAction(string action, string controller)
        {
            this.action = action;
            this.controller = controller;
        }
    }
}
